package com.molichuxing.services.infrastructure.service.impl;

import com.molichuxing.framework.exception.BizException;
import com.molichuxing.services.infrastructure.bean.WarnConfigBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.WarnConfigMapper;
import com.molichuxing.services.infrastructure.dto.request.modify.WarnConfigModifyDto;
import com.molichuxing.services.infrastructure.dto.response.WarnConfigDto;
import com.molichuxing.services.infrastructure.service.WarnConfigService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 保养表 服务实现类
 * </p>
 *
 * @author xuelin yi
 * @since 2019-09-23
 */
@Service("warnConfigService")
public class WarnConfigServiceImpl implements WarnConfigService {

    private static final Logger log = Logger.getLogger(WarnConfigServiceImpl.class);

    @Resource
    private WarnConfigMapper warnConfigMapper;


    /**
     * 修改配置信息
     *
     * @param list
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public Boolean modifyWarnConfig(List<WarnConfigModifyDto> list) throws Exception {
        if (list == null || list.size() < 1) {
            return true;
        }

        for (WarnConfigModifyDto warnConfigModifyDto : list) {
            Integer type = warnConfigModifyDto.getType();
            WarnConfigBean oldWarnConfigBean = warnConfigMapper.getByType(type);
            if (null == oldWarnConfigBean) {
                WarnConfigBean warnConfigBean = Convert.toWarnConfigBean(warnConfigModifyDto);
                warnConfigMapper.create(warnConfigBean);
            } else {
                WarnConfigBean warnConfigBean = Convert.toWarnConfigBean(warnConfigModifyDto, oldWarnConfigBean);
                warnConfigMapper.modifyByType(warnConfigBean);
            }
        }
        return true;
    }

    /**
     * 获取配置信息
     *
     * @return
     */
    @Override
    public List<WarnConfigDto> getList() {
        List<WarnConfigBean> list = warnConfigMapper.getList();
        return Convert.toWarnConfigDtoList(list);
    }

    /**
     * 修改配置信息
     *
     * @param warnConfigModifyDto
     * @return
     * @throws Exception
     */
    @Override
    public Boolean modifyWarnConfig(WarnConfigModifyDto warnConfigModifyDto) throws Exception {
        Integer type = warnConfigModifyDto.getType();
        if (null == type) {
            throw new BizException("请输入报警配置类型");
        }
        WarnConfigBean oldWarnConfigBean = warnConfigMapper.getByType(type);
        if (null == oldWarnConfigBean) {
            WarnConfigBean warnConfigBean = Convert.toWarnConfigBean(warnConfigModifyDto);
            warnConfigMapper.create(warnConfigBean);
        } else {
            WarnConfigBean warnConfigBean = Convert.toWarnConfigBean(warnConfigModifyDto, oldWarnConfigBean);
            warnConfigMapper.modifyByType(warnConfigBean);
        }

        return true;
    }


}
